package eai.tp2.lpco.database;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.OnDelete;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.OnDeleteAction;



@Entity
@Table(name = "USER")
@OnDelete(action = OnDeleteAction.CASCADE)
public class User implements Serializable {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	private String fullName;
	private String email;
	private String address;
	private List<Order> orders;

	public User() {
		
	}
	
	public User(String username, String password, String name, String email,
			String address) {
		this.username = username;
		this.password = password;
		this.fullName = name;
		this.email = email;
		this.address = address;
		this.orders = new ArrayList<Order>();
	}

	@Id
	@Column(name = "USERNAME", updatable = false)
	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	@Column(name = "PASSWORD")
	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	@Column(name = "FULLNAME")
	public String getFullName() {
		return fullName;
	}


	public void setFullName(String name) {
		this.fullName = name;
	}


	@Column(name = "EMAIL")
	public String getEmail() {
		return email;
	}


	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * DELETE_ORPHAN applies only to @OneToMany  associations, and indicates 
	 * that the delete()/remove() operation should be applied to any child 
	 * object that is removed from the association. In other words, if a 
	 * child is dereferenced by a persistent parent and if DELETE_ORPHAN 
	 * is used, the "orphaned" child is deleted.
	 * @return
	 */
    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name="username")
	public List<Order> getOrders() {
		return orders;
	}


	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
	
	@Column(name = "ADDRESS")	
	public String getAddress() {
		return address;
	}


	public void setAddress(String address) {
		this.address = address;
	}


	public String toString() {
		String res = "Username: " + username + "\n";
		res += "Name: " + fullName + "\n";
		res += "Address: " + address + "\n";
		res += "Email: " + email + "\n";
		res += "Number of orders: " + orders.size() + "\n";

		return res;
	}

	
}
